package com.atguigu.spzx.manager.service.impl;

import com.atguigu.spzx.manager.mapper.SysRoleMapper;
import com.atguigu.spzx.manager.service.SysRoleService;
import com.atguigu.spzx.model.entity.system.SysRole;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import jakarta.annotation.Resource;
import org.springframework.scheduling.annotation.Async;
import org.springframework.stereotype.Service;

import java.util.Date;
import java.util.List;
import java.util.concurrent.TimeUnit;

@Service
public class SysRoleServiceImpl implements SysRoleService {
    @Resource
    SysRoleMapper sysRoleMapper;
//    @Async
    @Override
    public PageInfo<SysRole> findPage(Integer pageNum, Integer pageSize, String keyword) {
        //开启分页
        PageHelper.startPage(pageNum,pageSize);
        //阻塞
//        try {
//            TimeUnit.SECONDS.sleep(2);
//        } catch (InterruptedException e) {
//            throw new RuntimeException(e);
//        }
        //实现分页查询的业务逻辑
        List<SysRole> sysRoleList = sysRoleMapper.findPage(keyword);
        return new PageInfo<SysRole>(sysRoleList);
    }

    @Override
    public void deleteById(Long id) {
        sysRoleMapper.deleteById(id);
    }

    @Override
    public void saveRole(SysRole sysRole) {
//        String roleName = sysRole.getRoleName();
//        String roleCode = sysRole.getRoleCode();


        // mp：时间日期的自动填充
        //创建更新时间需要在服务器中生成(mysql自己的时间可能存在时区问题)
        sysRole.setCreateTime(new Date());
        sysRole.setUpdateTime(new Date());
        sysRoleMapper.saveRole(sysRole);
    }

    @Override
    public SysRole getById(Long id) {

        return sysRoleMapper.getById(id);
    }

    @Override
    public void updateRole(SysRole sysRole) {
        sysRole.setUpdateTime(new Date());
        sysRoleMapper.updateRole(sysRole);
    }

    @Override
    public List<SysRole> findAllRole(Long userId) {

        return sysRoleMapper.findSysRolesAndCheckedStatusByUserId(userId);
    }
}
